package poswar.contribute.mbean;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.UploadedFile;

import poswar.contribute.util.Config;
import poswar.contribute.util.FacesUtils;

@ManagedBean
@RequestScoped
public class ShareBean {
	private static final Logger LOG = Logger.getLogger(ShareBean.class);

	private List<String> fileList;

	private String fileToDownload;
	
	@ManagedProperty("#{userBean}")
	private UserBean userBean;	

	public void initFileList() {
		File files = new File(Config.getSharePath());
		fileList = new ArrayList<String>();
		for (String file : files.list()) {
			fileList.add(file);
		}
	}

	public void upload(FileUploadEvent event) {
		UploadedFile file = event.getFile();
		try {
			LOG.debug("Uploading: " + file.getFileName());
			String name = Config.getSharePath() + "/" + userBean.getEmail() + "." + file.getFileName(); 
			FileOutputStream fos = new FileOutputStream(name);

			InputStream is = file.getInputstream();
			int BUFFER_SIZE = 8192;
			byte[] buffer = new byte[BUFFER_SIZE];
			int a;
			while (true) {
				a = is.read(buffer);
				if (a < 0)
					break;
				fos.write(buffer, 0, a);
				fos.flush();
			}
			fos.close();
			is.close();

			FacesMessage msg = new FacesMessage(event.getFile().getFileName()
					+ " is successfully uploaded.");
			FacesContext.getCurrentInstance().addMessage(null, msg);
			
			initFileList();
		} catch (IOException e) {
			String txtMsg = "Error uploading :" + file.getFileName();
			LOG.error(txtMsg, e);

			FacesUtils.addErrorMsg(txtMsg);
		} finally {

		}
	}

	public String remove() {
		File file = new File(Config.getSharePath() + "/" + fileToDownload);
		boolean test = file.delete();
		
		if (test) {
			FacesMessage msg = new FacesMessage(fileToDownload
					+ " is successfully removed.");
			FacesContext.getCurrentInstance().addMessage(null, msg);
			
			initFileList();
		}
		return null;
	}
	
	public StreamedContent getDownload() {
		LOG.debug("Downloading: " + fileToDownload);
		
		InputStream stream;
		try {
			stream = new FileInputStream(new File(Config.getSharePath() + "/" + fileToDownload));
			StreamedContent file = new DefaultStreamedContent(stream, null, fileToDownload);				
			return file;
		} catch (IOException e) {
			String txtMsg = "Error downloading :" + fileToDownload;
			LOG.error(txtMsg, e);

			FacesUtils.addErrorMsg(txtMsg);
			return null;
		}

	}

	public List<String> getFileList() {
		if (fileList == null) {
			initFileList();
		}
		return fileList;
	}

	public void setFileList(List<String> fileList) {
		this.fileList = fileList;
	}

	public String getFileToDownload() {
		return fileToDownload;
	}

	public void setFileToDownload(String fileToDownload) {
		this.fileToDownload = fileToDownload;
	}

	public UserBean getUserBean() {
		return userBean;
	}

	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}

	
}
